package distributedlock.redis;

import java.util.concurrent.atomic.AtomicInteger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class test2 {

	// redis.host=192.168.6.111
	// redis.port=6379
	// redis.password=
	// #redis.password=QvXtTZN4MyBDknj5

	private static AtomicInteger atomicInteger = new AtomicInteger();
	private static String host = "192.168.6.111";
	private static int port = 6379;

	public static void main(String[] args) throws InterruptedException {
		// JedisPool jedisPool = null;
		// jedisPool.getResource().setex("lock", 0, "1");

//		Jedis jedis = new Jedis("192.168.6.111", 6379);
		// jedis.set("key1", "test2");
		// System.out.println(jedis.mget("key","key1"));
		// jedis.set("key1", "zhangyue");
		// jedis.set("id", "1");
		// long id = jedis.incr("id");
		// String teString = jedis.get("key1");
		// System.out.println(id);
		// jedis.auth("admin");
		// jedis.incr("");
//		Pipeline p = jedis.pipelined();
//		p.set("key", "value");
//		p.get("key");
//
//		p.sync();
//
//		jedis.close();

		
		// JedisUitl jedisUitl = new JedisUitl(host, port);
		// System.out.println(jedisUitl.getId());

		System.out.println("test2");
//		Long start  = System.currentTimeMillis();
//		RedisLock redisLock = new RedisLock();
//		boolean islock = redisLock.lock();
//		if (islock) {
//			System.out.println("islock");
//			Thread.sleep(50 * 1000);
//			System.out.println("run end");
//		}
//		if ((System.currentTimeMillis() - start) / 1000 < RedisLock.EXPIRE) {
//			System.out.println("can unlock");
//			redisLock.unlock();
//		}
//		else {
//			System.out.println("time out");
//		}
		
		
		RedisLock2 redisLock2 = new RedisLock2();
		Long start = System.currentTimeMillis();
		try {
			if (redisLock2.lock()) { // 启用锁
				System.out.println("islock");
				Thread.sleep(8 * 1000);
			} else {
				System.out.println("wait for lock");
			}
		} catch (Throwable t) {
			// 分布式锁异常
			System.out.println(t.getMessage());
		} finally {
			if (redisLock2 != null && (System.currentTimeMillis() - start) < RedisLock2.LOCK_TIMEOUT) {
				System.out.println("can unlock");
				redisLock2.unlock();// 解锁
			}
			System.out.println("done");
		}
	
	
		
	}

}
